package com.reform.service.impl;

import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.MailService;
import com.reform.utils.RandomCodeUtils;
import com.reform.utils.RedisUtils;
import com.reform.utils.VerifycUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public Result sendVertifyCode(String to, String title, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 发送人
        message.setTo(to); // 收件人
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
        log.info("Mail content："+content+"to:"+to);
        return Result.success(ResultCode.SUCCESS);
    }

    /**
     * 发送邮箱验证码
     * @param to
     */
    @Override
    public Result sendVertifyCode(String to) {
        //验证是否参数缺省
        boolean verify = VerifycUtils.verifyNullOrEmptyUtils(to);
        if (!verify){
            return Result.failure(ResultCode.PARAM_IS_MISSING);
        }
        //验证邮箱是否合法
        boolean emailVerify = VerifycUtils.verifyEmailISLegal(to);
        if (!emailVerify){
            return Result.failure(ResultCode.PARAM_EMAIL_IS_WRONG);
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 发送人
        message.setTo(to); // 收件人
        message.setSubject("邮箱验证码");
        String content = RandomCodeUtils.getRandomCode();
        // 将邮箱验证码存入到redis中
        String code = (String) redisUtils.get(to);
        if (code != null){
            redisUtils.del(code);
        }
        // 5分钟内有效
        redisUtils.set(to,content,300);
        message.setText("邮箱验证码："+content+" ，五分钟内有效！");
        //发送
        mailSender.send(message);
        log.info("Mail 邮箱验证码："+content+" to:"+to+"");
        return Result.success(ResultCode.SUCCESS);
    }

    /**
     * 验证邮箱验证码
     * @param email
     * @param code
     * @return
     */
    @Override
    public Result vertifyCode(String email, String code) {
        //验证邮箱是否合法
        boolean emailVerify = VerifycUtils.verifyEmailISLegal(email);
        if (!emailVerify){
            return Result.failure(ResultCode.PARAM_EMAIL_IS_WRONG);
        }
        //校验验证码是否合法
        boolean codeVerify = VerifycUtils.verifyEmailCode(code);
        if (!codeVerify){
            return Result.failure(ResultCode.PARAM_EMAILCODE_IS_WRONG);
        }
        //从redis获取验证码
        String redis_code = (String) redisUtils.get(email);
        if (redis_code == null || !redis_code.equals(code)) {
            return Result.failure(ResultCode.PARAM_EMAILCODE_IS_WRONG);
        }
        return Result.success(ResultCode.SUCCESS);
    }
}
